-- Televation
-- Created by panyl Oct/27/2016
-- 电视机

-- 初始化
function initTelevation(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 查看残骸
    local function viewIronChest()
        -- 需要替换图片
        if buildingInfo["icon2"] and buildingInfo["icon2"] ~= "" then
            local changeIconPath = getLevelItemIconPath(buildingInfo["icon2"]);
            self.icon:loadTexture(changeIconPath);
        end

        DungeonActionM.go("do_building_bonus", self.gridData:getPos());

        EventMgr.fire(event.PLAYER_MOVE, self.gridData:getPos());
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("button_spell");

            if not DungeonM.isAllMonsterDead() then
                -- 需要击杀所有怪物方可拾取
                self:openBuildingView(nil, -20, buildingInfo["dialog2"], getLocStr("btn_text_leave"));
                return;
            end

            -- 打开建筑查看界面
            self:openBuildingView(viewIronChest);
        end
    end
    self.icon:addTouchEventListener(onClicked);
end